<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_car extends MY_Controller {

	public function __construct()
	{
			parent::__construct();
			$this->check_user();
			$this->load->model('User_car_model');
		
	}
	//查询列表
	public function index()
	{

			$start =  intval($this->input->get_post('per_page'));
 
			$params = array(
				'uid' =>$this->user_session['uid'] 
				,'start' => $start
				,'length'=>$this->page_size);


			$result = $this->User_car_model->select_page($params);
		
			$result['params'] = $params;

			//分页设置
			$base_url = site_url("c=user_car&m=index&username=$username&carno=$carno");
			$result['pagelink'] = $this->create_page_links($base_url,$result['count']);

			$this->load->template('user_car_list',$result);
	}

	public function manage()
	{
			$start =  intval($this->input->get_post('per_page'));
 
			$params = array(
				'uid' =>$this->user_session['uid'] 
				,'start' => $start
				,'length'=>$this->page_size);


			$result = $this->User_car_model->select_page($params);
		
			$result['params'] = $params;

			//分页设置
			$base_url = site_url("c=user_car&m=index&username=$username&carno=$carno");
			$result['pagelink'] = $this->create_page_links($base_url,$result['count']);

			$this->load->template('user_car_manage',$result);
	}
	 
	//增加保养车辆
	public function add()
	{
		$this->form_validation->set_rules('carno', '车牌号', 'required');
		
		$careditsubmit = $this->input->post('careditsubmit');
		$resultinfo  =array('method'=>'add');
		if($careditsubmit  && $this->form_validation->run())
		{
			$data = array('uid'=>$this->user_session['uid'],'status'=>0);

			$data['carno'] = $this->input->get_post('carno');
			$data['carid'] = $this->input->get_post('carid');
			$data['brandid'] = $this->input->get_post('brandid');
			$data['seriesid'] = $this->input->get_post('seriesid');
			$data['enginelicense'] = $this->input->get_post('enginelicense');
			$data['driverlicense'] = $this->input->get_post('driverlicense');
 
			$result = $this->User_car_model->insert($data);

			$this->show_message("新增车辆信息成功",site_url("c=user_car&m=manage"));
			
		}
		else
		{		
			$this->load->model('Car_brand_model');
			$this->load->model('Car_series_model');
			$resultinfo['brandlist'] =$this->Car_brand_model->select_list();
			$resultinfo['serieslist'] =$this->Car_series_model->select_list_group_by_series();
			$this->load->template('user_car_edit',$resultinfo);
			
		}
	}

	//增加保养车辆
	public function edit()
	{
		$this->form_validation->set_rules('carno', '车牌号', 'required');

		$id = $this->input->get_post('id');
		$careditsubmit = $this->input->post('careditsubmit');
		$resultinfo  =array('method'=>'edit');
		$resultinfo['carinfo'] = $this->User_car_model->select_by_id($id);
		if($careditsubmit  && $this->form_validation->run())
		{
			$data = array('status'=>0);

			$data['carno'] = $this->input->get_post('carno');
			$data['carid'] = $this->input->get_post('carid');
			$data['brandid'] = $this->input->get_post('brandid');
			$data['seriesid'] = $this->input->get_post('seriesid');
			$data['enginelicense'] = $this->input->get_post('enginelicense');
			$data['driverlicense'] = $this->input->get_post('driverlicense');
 
			$result = $this->User_car_model->update($id,$data);

			$this->show_message("修改车辆信息成功",site_url("c=user_car&m=manage"));
			
		}
		else
		{		
			$this->load->model('Car_brand_model');
			$this->load->model('Car_series_model');
			$resultinfo['brandlist'] =$this->Car_brand_model->select_list();
			$resultinfo['serieslist'] =$this->Car_series_model->select_list_group_by_series();
			$this->load->template('user_car_edit',$resultinfo);
			
		}
	}
	//删除用户车辆信息
	public function delete()
	{
		$id = $this->input->get_post('id');
		$result = $this->User_car_model->delete($id);
		if($result['success'])
		{
			$this->show_message("删除车辆类型信息成功",site_url("c=user_car&m=manage"));
		}
		else
		{
			$this->show_message($result['message'],site_url("c=user_car&m=manage"));
		}
	}
	
	//获取车款列表
	public function json_catlist()
	{
		$this->load->model('Car_info_model');
		$brandid = $this->input->get_post('brandid');
		$seriesid = $this->input->get_post('seriesid');
		$result = $this->Car_info_model->select_car_list($brandid ,$seriesid);
		$result['success']=true;
		$this->load->library('json');
		echo $this->json->encode($result);
		
	}
 
}